package cc.autoapi.pucong.auto.flow.spring.boot.start.config;

import cc.autoapi.pucong.auto.flow.core.flow.execNode.exec.AutoFlow;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.task.TaskFactory;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.task.extend.HttpTask;
import cc.autoapi.pucong.auto.flow.persistence.repository.api.ApiInfoService;
import cc.autoapi.pucong.auto.flow.persistence.repository.properties.PropertiesInfoService;
import cc.autoapi.pucong.auto.flow.provider.aop.manager.AopInfoManager;
import cc.autoapi.pucong.auto.flow.provider.api.manager.ApiHandler;
import cc.autoapi.pucong.auto.flow.provider.api.manager.ApiNodeLogHandler;
import cc.autoapi.pucong.auto.flow.provider.log.manager.LogManager;
import cc.autoapi.pucong.auto.flow.spring.boot.start.extTask.AiChatTask;
import cc.autoapi.pucong.auto.flow.spring.boot.start.extTask.AiToolsTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.annotation.Resource;

@Configuration
@ComponentScan("cc.autoapi.pucong.auto.flow")
@EnableScheduling
@EnableAsync
@Slf4j
public class FlowStart implements ApplicationRunner, DisposableBean {

    @Resource
    private ApiInfoService apiInfoService;

    @Resource
    private AopInfoManager aopInfoManager;

    @Resource
    private ApiHandler apiHandler;

    @Resource
    private ApiNodeLogHandler apiNodeLogHandler;

    @Resource
    private LogManager logManager;

    @Resource
    private PropertiesInfoService propertiesInfoService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 初始化发布接口
        apiInfoService.initPublishApi();
        aopInfoManager.initPublishAop();
        // 初始化执行处理器
        AutoFlow autoFlow = new AutoFlow();
        autoFlow.registry(apiHandler);
        autoFlow.registry(apiNodeLogHandler);

        TaskFactory.registerTask("HttpTask", HttpTask.class);
        TaskFactory.registerTask("aiToolsTask", AiToolsTask.class);
        TaskFactory.registerTask("aiChatTask", AiChatTask.class);
        propertiesInfoService.initCache();
        // 初始化日志处理
        logManager.initLogDeal();
    }

    @Override
    public void destroy() throws Exception {

    }
}
